package com.netkit.message;

import java.nio.ByteBuffer;

import org.jboss.netty.buffer.ChannelBuffer;

public class MessageEncoder {

	public static byte[] encode(Message message){
		ByteBuffer buffer = ByteBuffer.allocate(message.getPacketSize());
		encode(buffer,message);	
		byte[] bytes =  buffer.array();
		buffer.clear();
		return bytes;
	}
	
	public static void encode(ByteBuffer buffer,Message message){
		buffer.putInt(message.getMessageId());
		buffer.putInt(message.getContentLength());
		if(message.getContentLength() > 0){
			buffer.put(message.getMessageContent());
		}		
	}
	
	public static void encode(ChannelBuffer buffer,Message message){
		buffer.writeInt(message.getMessageId());
		buffer.writeInt(message.getContentLength());
		if(message.getContentLength() > 0){
			buffer.writeBytes(message.getMessageContent());
		}
	}
}
